from basic_event import basic_event
from almanaque import Almanaque 
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

import icalendar
import unittest

class TestMyModule(unittest.TestCase):

	#Yearly
	def test_FREQ_YEARLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['YEARLY']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_BYDAY_YEARLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['YEARLY'], 'BYDAY': ['2']})))
		self.assertEqual(a.freq(0, datetime.today()), True)
		self.assertTrue(a.freq(0, datetime.today()))

	

	def test_FREQ_BYDAYMONTH_YEARLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['YEARLY'], 'BYMONTHDAY' : ['2']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_OTHER_YEARLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['YEARLY']})))
		self.assertFalse(a.freq(0, datetime.today() + timedelta(1)))

	def test_FREQ_INTERVAL_YEARLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['YEARLY'], 'INTERVAL': [2]})))
		date1 = datetime.today() + relativedelta(years=+2)
		self.assertTrue(a.freq(0, date1))

	#Monthly

	def test_FREQ_MONTHLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['MONTHLY']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_BYDAY_MONTHLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['MONTHLY'], 'BYDAY': ['2']})))
		self.assertEqual(a.freq(0, datetime.today()), True)
		self.assertTrue(a.freq(0, datetime.today()))

	

	def test_FREQ_BYDAYMONTH_MONTHLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['MONTHLY'], 'BYMONTHDAY' : ['2']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_OTHER_MONTHLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['MONTHLY']})))
		self.assertFalse(a.freq(0, datetime.today() + timedelta(1)))

	def test_FREQ_INTERVAL_MONTHLY(self):
		a = Almanaque()
		today = datetime(1956, 12, 5)
		
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['MONTHLY'], 'INTERVAL': [2]})))
		date1 = today + relativedelta(months=2) 
		self.assertTrue(a.freq(0, date1))

	#WEEKLY

	def test_FREQ_WEEKLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['WEEKLY']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_BYDAY_WEEKLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYDAY': ['2']})))
		self.assertEqual(a.freq(0, datetime.today()), True)
		self.assertTrue(a.freq(0, datetime.today()))

	

	def test_FREQ_BYDAYMONTH_WEEKLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYMONTHDAY' : ['2']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_OTHER_WEEKLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['WEEKLY']})))
		self.assertFalse(a.freq(0, datetime.today() + timedelta(1)))

	def test_FREQ_INTERVAL_WEEKLY(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['WEEKLY'], 'INTERVAL': [2]})))
		date1 = datetime.today() + relativedelta(weeks=+2)
		self.assertTrue(a.freq(0, date1))

	#DAILY

	def test_FREQ(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['DAILY']})))
		self.assertTrue(a.freq(0, datetime.today()))

	def test_FREQ_INTERVAL(self):
		a = Almanaque()
		
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['DAILY'], 'INTERVAL': [2]})))
		date1 = datetime.today() + relativedelta(days=+2)
		self.assertTrue(a.freq(0, date1))

	#BYYEARDAY

	def test_BYYEARDAY(self):
		a = Almanaque()
		today = datetime(2020, 7, 31)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['DAILY'], 'BYYEARDAY': 213})))

		self.assertTrue(a.byyearday(0, today))


	#BYWEEKNO
	def test_BYWEEKNO(self):
		a = Almanaque()
		today = datetime(2020, 7, 31)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['DAILY'], 'BYWEEKNO': 31})))

		self.assertTrue(a.byweekno(0, today))
	
	#BYSECOND

	def test_BYSECOND(self):
		a = Almanaque()
		today = datetime(2020, 7, 31,18,22,23)
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['DAILY'], 'BYSECOND': 23})))

		self.assertTrue(a.bysecond(0, today))

	#BYYEAR

	def test_BYYEAR(self):
		a = Almanaque()
		today = datetime(2020, 7, 31)
		a.event_list.append(basic_event("test", datetime.today(), datetime.today(),  icalendar.vRecur({'FREQ': ['DAILY'], 'BYYEAR': 2020})))

		self.assertTrue(a.byyear(0, datetime.today()))

	#check_byday_array

	def test_byday_array(self):
		a = Almanaque()
		array = []
		days = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
		i = 0
		today = datetime(2020, 8, 3)
		a.event_list.append(basic_event("test", today, today, icalendar.vRecur({'FREQ': ['DAILY'], 'BYDAY': 2020})))
		while(i < len(days)):
			temporal_array = []
			a.event_list[0].start = today
			temporal_array.append(days[i])
			self.assertTrue(a.check_byday_array(temporal_array, i, today))
			today = today + timedelta(1)
			i += 1


	def test_byday_array_week(self):
		a = Almanaque()
		i = 1
		today = datetime(2020, 7, 1)
		a.event_list.append(basic_event("test", today, today, icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYDAY': ['1WE']})))
		while(i < 6):
			temporal_array = []
			rrule_byday = str(i)+ 'WE'

			temporal_array.append(rrule_byday)

			self.assertTrue(a.check_byday_array(temporal_array, 2, today))
			today = today + timedelta(7)
			i += 1

	#check_week_number

	def test_check_week_number(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYDAY': ['1WE']})))

		self.assertTrue(a.check_week_number(2, 1, today))
	
	#byday

	def test_byday_method(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYDAY': ['1WE']})))

		self.assertTrue(a.byday(0, today))

	#bymonthday

	def test_bymonthday_method(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYMONTHDAY': [5]})))

		self.assertTrue(a.bymonthday(0, today))

	#bymonth

	def test_bymonth_method(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'BYMONTH': [8]})))

		self.assertTrue(a.bymonth(0, today))

	#bymonth

	def test_until(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'UNTIL': [datetime(2020, 8, 8, 0, 0)]})))

		self.assertTrue(a.until(0, today))
		self.assertFalse(a.until(0, today + timedelta(20)))

	def test_count(self):
		a = Almanaque()
		today = datetime(2020, 8, 5)
		a.event_list.append(basic_event("test", today, today,  icalendar.vRecur({'FREQ': ['WEEKLY'], 'COUNT': [2]})))

		self.assertTrue(a.count(0))

if __name__ == "__main__":
	unittest.main()


